<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="utf-8">
    <title>电影管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="shortcut icon" th:href="@{/icon/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}">
</head>
<body class="layui-layout-body">
<div id="LAY_app">
    <div class="layui-layout layui-layout-admin">
        <!--头部导航-->
        <div th:replace="~{admin/public/header::header}"></div>
        <!--侧边菜单-->
        <div th:replace="~{admin/public/sider::sider('movieList','movie')}"></div>

        <!-- 主体内容 -->
        <div class="layui-body" id="LAY_app_body" style="margin-top: -50px">
            <div class="layui-fluid">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div style="padding-bottom: 10px;">
                            <button class="layui-btn layui-btn-danger" data-type="batchdel" id="batchDel"><i
                                    class="layui-icon layui-icon-delete"></i>批量删除
                            </button>
                        </div>

                        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
                        <script type="text/html" id="table-useradmin-webuser">
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                                    class="layui-icon layui-icon-edit"></i>编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                                    class="layui-icon layui-icon-delete"></i>删除</a>
                            <a class="layui-btn  layui-btn-xs" lay-event="pre"><i
                                    class="layui-icon layui-icon-file"></i>预览</a>
                        </script>
                    </div>
                </div>
            </div>
        </div>

        <!-- 辅助元素，一般用于移动设备下遮罩 -->
        <div class="layadmin-body-shade" layadmin-event="shade"></div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script type="text/javascript">
    //显示大图片
    function showBigImage(e) {
        console.log($(e));
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true, //点击阴影关闭
            offset: ['100px'],
            content: "<img src=" + $(e).attr('src') + " />"
        });
    }
</script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'layer', 'table'], function () {
        var $ = layui.$
            , element = layui.element
            , form = layui.form
            , table = layui.table
            , layer = layui.layer;

        form.render();
        //初始化表格数据
        let myTable = table.render({
            elem: "#LAY-user-manage",
            url: "/admin/tableMovie",
            limit: 5,  //要传向后台的每页显示条数
            page: {   //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']//自定义分页布局
                , limits: [5, 10, 15, 20, 25, 30, 35]
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id", title: "ID", sort: !0},
                    {field: "title", title: "电影标题", minWidth: 100},
                    {field: "author", title: "导演", minWidth: 100},
                    {
                        field: "createDate",
                        title: "发布时间",
                        sort: !0,
                        templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                    },
                    {field:"headerImageUrl",title:"海报",templet:"<div id='x-img'><img  src='{{d.headerImageUrl}}' onclick='showBigImage(this)'/></div>"},
                    {field:"description",title:"简介"},
                    {title: "操作", align: "center", fixed: "right", toolbar: "#table-useradmin-webuser"}
                ]
            ],
            height: "full-220",
            text:
                {
                    none: '结果为空'
                }
        });

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            let title = $('input[name="title"]').val();
            let type = $('#type').val();
            let checkbox = document.getElementById('checkbox');
            let flag = checkbox.checked;

            //执行重载
            myTable.reload({
                where:
                    {
                      'title' : title,
                      'typeId' : type,
                      'isCommend' : flag
                    },
                page: 1
            });
            return false;
        });

        $('#batchDel').on('click', function () {
            var checkStatus = table.checkStatus('LAY-user-manage')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据',{
                    icon: 5
                });
            }
            layer.confirm('确定删除吗？', function (index) {
                let array = [];
                for (let i = 0; i < checkData.length; i++) {
                    array[i] = checkData[i].id;
                }
                alert(array);
                //执行 Ajax 后重载
                $.ajax({
                    type: 'POST',
                    url: '/admin/deleteMovies',
                    data:
                        {
                            'blogs': array
                        },
                    success: function (data) {
                        let json;
                        if ((typeof data === 'string') && data.constructor === String) {
                            if (data.charAt(0) === '<') {
                                layer.msg('服务器异常', {
                                    icon: 5
                                });
                                return;
                            }
                            json = JSON.parse(data);
                        } else {
                            json = data;
                        }
                        if (json.code === 1) {
                            layer.msg('删除成功', {
                                icon: 6
                            });
                            let title = $('input[name="title"]').val();
                            let type = $('#type').val();
                            let checkbox = document.getElementById('checkbox');
                            let flag = checkbox.checked;
                            myTable.reload({
                                where:
                                    {
                                        'title': title,
                                        'type' : type,
                                        'commend' : flag
                                    },
                                page:
                                    {
                                        curr: $(".layui-laypage-em").next().html()
                                    }
                            });
                        } else if (json.code === 2) {
                            layer.msg(json.msg, {
                                icon: 5
                            });
                        }
                    },
                    error: function () {
                        layer.msg('请求异常,请稍后重试', {
                            icon: 5
                        })
                    }
                });
                table.reload('LAY-user-manage');
                layer.msg('已删除');
            });
        });
        //工具栏事件
        table.on("tool(LAY-user-manage)", function (obj) {
            if ("del" === obj.event) layer.confirm("真的删除这一行吗", function (t) {
                $.ajax({
                    type: 'GET',
                    url: '/admin/deleteMovie/'+obj.data.id,
                    data: {},
                    success: function (data) {
                        let json;
                        if ((typeof data === 'string') && data.constructor === String) {
                            if (data.charAt(0) === '<') {
                                layer.msg('服务器异常', {
                                    icon: 5
                                });
                                return;
                            }
                            json = JSON.parse(data);
                        } else {
                            json = data;
                        }
                        if (json.code === 1) {
                            layer.msg('删除成功', {
                                icon: 6
                            });
                            obj.del();
                            layer.close(t);
                        } else if (json.code === 2) {
                            layer.msg(json.msg, {
                                icon: 5
                            });
                        }
                    },
                    error: function () {
                        layer.msg('请求异常,请稍后重试', {
                            icon: 5
                        })
                    }
                });
            });else if ("pre" === obj.event){
                parent.layer.open({
                    type: 2,
                    title: '预览',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['50%', '60%'],
                    content: '/admin/movie/preMovie/'+obj.data.id,
                    maxmin: true,
                    zIndex: layer.zIndex,
                    success: function (layero) {
                        layer.setTop(layero);
                    }
                });
            }else if ('edit' === obj.event){
                parent.layer.open({
                    type: 2,
                    title: '修改电影信息',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['50%', '90%'],
                    content: '/admin/movie/editMovie/'+obj.data.id,
                    maxmin: true,
                    zIndex: layer.zIndex,
                    success: function (layero) {
                        table.reload('LAY-user-manage');
                        layer.setTop(layero);
                    }
                });
            }
        });
    });
</script>
</body>
</html>
